﻿#include <mutex>
#include "SLT_timer_imp.h"
//#include "evutil.h"
#include "SLT_timer.h"

static std::mutex  g_stl_timer_mutex;

int SLT_setAsynFrameTimer(SLT_TIMER_HANDLE& _th, int _intervalMs, bool periodic, SLT_timerTimeOut _fun, void* _userData)
{
	static LibeventTimer* g_AsynFrameTimer_220424 = NULL;  //异步模型定时器
	if (NULL == g_AsynFrameTimer_220424) {
		g_stl_timer_mutex.lock();
		if (NULL == g_AsynFrameTimer_220424) {
			LibeventTimer::init();
			LibeventTimer* temp = new LibeventTimer();
			temp->start();
			g_AsynFrameTimer_220424 = temp;

		}
		g_stl_timer_mutex.unlock();
	}

	return g_AsynFrameTimer_220424->set_timer(_th, _intervalMs, periodic, _fun, _userData);
}
SLT_API int SLT_setTimer(SLT_TIMER_HANDLE& _th, int _intervalMs, bool periodic, SLT_timerTimeOut _fun, void* _userData)
{
	static LibeventTimer* g_LibeventTimer_220103 = NULL; //全局定时器
	if (NULL == g_LibeventTimer_220103) {
		g_stl_timer_mutex.lock();
		if (NULL == g_LibeventTimer_220103) {
			LibeventTimer::init();
			LibeventTimer* temp = new LibeventTimer();
			temp->start();
			g_LibeventTimer_220103 = temp;
		}
		g_stl_timer_mutex.unlock();
	}
	
	//return g_LibeventTimer_220103->set_timer(_intervalMs, false, (SLT_libeventTimerTimeOut)_fun, _userData);
	return g_LibeventTimer_220103->set_timer(_th,_intervalMs, periodic, _fun, _userData);
}

SLT_API void SLT_getTimeOfDay(struct timeval* _time)
{
	evutil_gettimeofday(_time, nullptr);
}
SLT_API void SLT_localTime(time_t timep, struct tm* result)
{
#ifdef WIN32
	localtime_s(result,&timep );
#else
	localtime_r(&timep, result);
#endif
}
